﻿// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

namespace Accord.MachineLearning
{
    using System;
    using Accord.Math;
    using Accord.Compat;

    /// <summary>
    ///   Base class for data transformation algorithms.
    /// </summary>
    /// 
    /// <typeparam name="TInput">The type for the output data that enters in the model. Default is double[].</typeparam>
    /// <typeparam name="TOutput">The type for the input data that exits from the model. Default is double[].</typeparam>
    /// 
    [Serializable]
    public abstract class TransformBase<TInput, TOutput> : ITransform<TInput, TOutput>
    {
        private int inputs;
        private int outputs;

        /// <summary>
        ///   Gets the number of inputs accepted by the model.
        /// </summary>
        /// 
        public virtual int NumberOfInputs
        {
            get { return inputs; }
            set { inputs = value; }
        }

        /// <summary>
        ///   Gets the number of outputs generated by the model.
        /// </summary>
        /// 
        public virtual int NumberOfOutputs
        {
            get { return outputs; }
            set { outputs = value; }
        }

        /// <summary>
        ///   Applies the transformation to an input, producing an associated output.
        /// </summary>
        /// 
        /// <param name="input">The input data to which the transformation should be applied.</param>
        /// 
        /// <returns>The output generated by applying this transformation to the given input.</returns>
        /// 
        public abstract TOutput Transform(TInput input);

        /// <summary>
        ///   Applies the transformation to a set of input vectors,
        ///   producing an associated set of output vectors.
        /// </summary>
        /// 
        /// <param name="input">The input data to which
        ///   the transformation should be applied.</param>
        /// 
        /// <returns>The output generated by applying this
        ///   transformation to the given input.</returns>
        /// 
        public virtual TOutput[] Transform(TInput[] input)
        {
            return Transform(input, new TOutput[input.Length]);
        }

        /// <summary>
        ///   Applies the transformation to an input, producing an associated output.
        /// </summary>
        /// 
        /// <param name="input">The input data to which the transformation should be applied.</param>
        /// <param name="result">The location to where to store the result of this transformation.</param>
        /// 
        /// <returns>The output generated by applying this transformation to the given input.</returns>
        /// 
        public virtual TOutput[] Transform(TInput[] input, TOutput[] result)
        {
            for (int i = 0; i < input.Length; i++)
                result[i] = Transform(input[i]);
            return result;
        }
    }

    /// <summary>
    ///   Base class for data transformation algorithms.
    /// </summary>
    /// 
    /// <typeparam name="TInput">The type for the output data that enters in the model. Default is double[].</typeparam>
    /// <typeparam name="TOutput">The type for the input data that exits from the model. Default is double[].</typeparam>
    /// 
    [Serializable]
    public abstract class MultipleTransformBase<TInput, TOutput> : TransformBase<TInput, TOutput[]>
    {
        /// <summary>
        ///   Applies the transformation to an input, producing an associated output.
        /// </summary>
        /// 
        /// <param name="input">The input data to which the transformation should be applied.</param>
        /// 
        /// <returns>The output generated by applying this transformation to the given input.</returns>
        /// 
        public override TOutput[] Transform(TInput input)
        {
            return Transform(input, new TOutput[NumberOfOutputs]);
        }

        /// <summary>
        ///   Applies the transformation to an input, producing an associated output.
        /// </summary>
        /// 
        /// <param name="input">The input data to which the transformation should be applied.</param>
        /// <param name="result">The location to where to store the result of this transformation.</param>
        /// 
        /// <returns>The output generated by applying this transformation to the given input.</returns>
        /// 
        public virtual TOutput[] Transform(TInput input, TOutput[] result)
        {
            return Transform(new[] { input }, new[] { result })[0];
        }

        /// <summary>
        ///   Applies the transformation to a set of input vectors,
        ///   producing an associated set of output vectors.
        /// </summary>
        /// 
        /// <param name="input">The input data to which
        ///   the transformation should be applied.</param>
        /// 
        /// <returns>The output generated by applying this
        ///   transformation to the given input.</returns>
        /// 
        public override TOutput[][] Transform(TInput[] input)
        {
            return Transform(input, Jagged.Create<TOutput>(input.Length, NumberOfOutputs));
        }

        /// <summary>
        ///   Applies the transformation to an input, producing an associated output.
        /// </summary>
        /// 
        /// <param name="input">The input data to which the transformation should be applied.</param>
        /// <param name="result">The location to where to store the result of this transformation.</param>
        /// 
        /// <returns>The output generated by applying this transformation to the given input.</returns>
        /// 
        public override TOutput[][] Transform(TInput[] input, TOutput[][] result)
        {
            for (int i = 0; i < input.Length; i++)
                Transform(input[i], result[i]);
            return result;
        }
    }

    /// <summary>
    ///   Base class for data transformation algorithms.
    /// </summary>
    /// 
    /// <typeparam name="TInput">The type for the output data that enters in the model. Default is double[].</typeparam>
    /// 
    [Serializable]
    public abstract class TransformBase<TInput> : TransformBase<TInput, double>
    {
    }

    /// <summary>
    ///   Base class for data transformation algorithms.
    /// </summary>
    /// 
    [Serializable]
    public abstract class TransformBase : TransformBase<double[]>
    {
    }
}
